package com.company.site.mode.common;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class Page<T> {

	public static final int  DEFAULT_PAGESIZA=6;
	
	protected int pageNo=1;
	protected int pageSize=DEFAULT_PAGESIZA;
	protected int index=-1;
	
	protected List<T> result=new ArrayList<T>();
	protected long totalCount=-1;
	protected LinkedHashMap<String, Object> filter = new LinkedHashMap<String, Object>();
	
	public Page(){
		
	}
	
	public Page(int pageSize){
		this.pageSize=pageSize;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(final int pageNo) {
		this.pageNo = pageNo;
		if(this.pageNo<1){
			this.pageNo=1;
		}
	}
	
	public Page<T> pageNo(final int thePageNo) {
		setPageNo(thePageNo);
		return this;
	}

	public int getFirst() {
		return ((pageNo - 1) * pageSize) + 1;
	}

    public int getIndex() {
        return index==-1 ? (pageNo - 1) * pageSize : index;
    }
	
	public int getStart() {
		return getIndex();
	}
	
	public int getEnd(){
		return this.getStart()+this.getPageSize();
	}
	
    public long getTotalPages(){
    	if(totalCount<1){
    		return 0;
    	}
    	long count=totalCount/pageSize;
    	if(totalCount % pageSize > 0){
    		count++;
    	}
    	return count;
    }
       
    
    public boolean isHasNext(){
    	return (pageNo+1<=getTotalPages());
    }
    
    public int getNextPage(){
    	if(isHasNext()){
    		return pageNo+1;
    	}else{
    		return pageNo;
    	}
    }
    
    public boolean isHasPre(){
    	return (pageNo-1>=1);
    }
    
    
    public int getPrePage(){
    	if(isHasPre()){
    		return pageNo-1;
    	}else{
    		return pageNo;
    	}
    }
    
    
	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public List<T> getResult() {
		return result;
	}

	public void setResult(final List<T> result) {
		this.result = result;
	}

	public long getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(final long totalCount) {
		this.totalCount = totalCount;
	}

	public LinkedHashMap<String, Object> getFilter() {
		return filter;
	}

	public void setFilter(LinkedHashMap<String, Object> filter) {
		this.filter = filter;
	}
	
}
